package com.ytkj.postman2doc.gendoc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.DocxRenderData;
import com.deepoove.poi.data.RowRenderData;
import com.ytkj.postman2doc.gendoc.data.DataItem;
import com.ytkj.postman2doc.gendoc.data.DetailPolicy;
import com.ytkj.postman2doc.gendoc.data.PostmanData;
import com.ytkj.postman2doc.model.CollectionModel;
import com.ytkj.postman2doc.model.HeaderItem;
import com.ytkj.postman2doc.model.Item;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: MarkBell
 * @Description:
 * @Date 2019/3/1
 */
@Slf4j
public class GenDoc {

    public static void genDocByCollectionModel(CollectionModel collectionModel, HttpServletResponse response) throws IOException {
        File dataTemplate=ResourceUtils.getFile("classpath:dataTemplate.docx");
        File itemTemplate = ResourceUtils.getFile("classpath:itemTemplate.docx");
        PostmanData postmanData = new PostmanData();
        List<Item> items = new ArrayList<>();
        collectionModel.getItem().forEach(postmanItem -> {
            if(postmanItem!=null&&postmanItem.getItem()!=null){
                items.addAll(postmanItem.getItem());
            }
        });
        List<DataItem>  dataItems = new ArrayList<>();
        for(Item item:items){
            DataItem dataItem = new DataItem();
            dataItem.setDescription(item.getName());
            dataItem.setUrl(item.getRequest().getUrl());
            dataItem.setMethod(item.getRequest().getMethod());

            //添加header
            List<HeaderItem> headerItems = item.getRequest().getHeader();
            List<RowRenderData> headerItemRow = new ArrayList<>(headerItems.size());
            for(HeaderItem headerItem:headerItems){
                RowRenderData data = RowRenderData.build("请求头",headerItem.getKey(),headerItem.getValue());
                headerItemRow.add(data);
            }
            dataItem.getHeaderData().setReqHeaders(headerItemRow);
            //添加请求参数
            String row = item.getRequest().getBody().getRaw();
            if(row!=null){
                dataItem.setReqParamsExample(row);
                JSONObject jsonObject = JSON.parseObject(row);
                if(jsonObject!=null){
                    List<RowRenderData> reqParamsRow = new ArrayList<>(jsonObject.keySet().size());
                    for(String key:jsonObject.keySet()){
                        RowRenderData data = RowRenderData.build("请求参数",key,jsonObject.getString(key));
                        reqParamsRow.add(data);
                    }
                    dataItem.getHeaderData().setReqParams(reqParamsRow);
                }

            }
            dataItems.add(dataItem);
        }
        DocxRenderData item = new DocxRenderData(itemTemplate,dataItems);
        postmanData.setItem(item);
        Configure config = Configure.newBuilder().customPolicy("headerData", new DetailPolicy()).build();
        XWPFTemplate template = XWPFTemplate.compile(dataTemplate,config).render(postmanData);
        response.setHeader("Content-disposition", "attachment; filename=" + "interface"+ ".docx");
        OutputStream out=response.getOutputStream();
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }
}
